﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ControlLibrary
{
    public partial class RichTxtTools : UserControl
    {
        public RichTxtTools()
        {
            InitializeComponent();
        }

        public RichTextBox RichText { get; set; }

        Encoding ec = Encoding.UTF8;  //设置文本的格式为 UTF-8
        private void openFileToolStripButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "文本文件|*.txt;*.rtf"; //文件打开的过滤器
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string title = openFileDialog1.FileName;
                RichText.Modified = false;
                string ext = title.Substring(title.LastIndexOf(".") + 1);//获取文件格式
                ext = ext.ToLower();
                FileStream fs = new FileStream(title, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, ec);
                if (ext == "rtf")  //如果后缀是 rtf 加载文件进来
                {
                    RichText.LoadFile(title, RichTextBoxStreamType.RichText);
                }
                else
                {
                    RichText.Text = sr.ReadToEnd();
                }
                fs.Close();
                sr.Close();
            }
        }

        private void toolStripButton14_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();      
            saveFileDialog1.Filter = "RTF Files|*.rtf|文本文件（*.txt）|*.txt";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string title = saveFileDialog1.FileName;
                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:
                        RichText.SaveFile(title, RichTextBoxStreamType.RichText);
                        break;
                    case 2:
                        RichText.SaveFile(title, RichTextBoxStreamType.PlainText);
                        break;
                    default:
                        RichText.SaveFile(title, RichTextBoxStreamType.RichNoOleObjs);
                        break;
                }
                RichText.Modified = false;
            }
        }

        private void boldToolStripButton_Click(object sender, EventArgs e)
        {
            RichText.SelectionFont = new Font(RichText.SelectionFont, RichText.SelectionFont.Style ^ FontStyle.Bold);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            RichText.SelectionFont = new Font(RichText.SelectionFont, RichText.SelectionFont.Style ^ FontStyle.Italic);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            RichText.SelectionFont = new Font(RichText.SelectionFont, RichText.SelectionFont.Style ^ FontStyle.Underline);
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            RichText.Cut();
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            RichText.Copy();
        }

        private void toolStripButton12_Click(object sender, EventArgs e)
        {
            RichText.Paste();
        }

        private void toolStripButton13_Click(object sender, EventArgs e)
        {
            OpenFileDialog openImageDialog = new OpenFileDialog();
            Bitmap bmp;
            if (openImageDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = openImageDialog.FileName;
                try
                {
                    bmp = new Bitmap(filename);//文件转化为 Bitmap
                    Clipboard.SetDataObject(bmp);
                    DataFormats.Format dft = DataFormats.GetFormat(DataFormats.Bitmap);
                    if (RichText.CanPaste(dft))
                        RichText.Paste(dft);    //图片加入到富文本中去
                }
                catch (Exception ex)
                {
                    MessageBox.Show("图片插入失败" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void toolStripButton15_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                RichText.SelectionColor = colorDialog1.Color;//直接设置选中的字段的颜色
            }
        }

        private void toolStripButton19_Click(object sender, EventArgs e)
        {
            RichText.Undo();
        }

        private void toolStripButton20_Click(object sender, EventArgs e)
        {
            RichText.Redo();
        }

        private void toolStripButton21_Click(object sender, EventArgs e)
        {
            RichText.SelectionAlignment = HorizontalAlignment.Left;
        }

        private void toolStripButton22_Click(object sender, EventArgs e)
        {
            RichText.SelectionAlignment = HorizontalAlignment.Center;
        }

        private void toolStripButton23_Click(object sender, EventArgs e)
        {
            RichText.SelectionAlignment = HorizontalAlignment.Right;
        }

        private void ZoomInOut(bool IsZoomOut)
        {
            float zoom = 0;
            zoom = RichText.ZoomFactor;
            if (IsZoomOut)
                zoom -= (float)0.1;
            else
                zoom += (float)0.1;
            if (zoom < 0.64 || zoom > 64)
                return;
            RichText.ZoomFactor = zoom;
        }

        private void toolStripButton25_Click(object sender, EventArgs e)
        {
            ZoomInOut(false);
        }

        private void toolStripButton26_Click(object sender, EventArgs e)
        {
            ZoomInOut(true);
        }

        private void cbFamily_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbFamily.SelectedItem != null)
            {
                string ss = this.cbFamily.SelectedItem.ToString().Trim();
                RichText.SelectionFont = new Font(ss, RichText.SelectionFont.Size, RichText.SelectionFont.Style);
            }
        }

        private void cbSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSize.SelectedItem != null)
            {
                int size = Convert.ToInt32(this.cbSize.SelectedItem.ToString().Trim());
                RichText.SelectionFont = new Font(RichText.SelectionFont.FontFamily, size, RichText.SelectionFont.Style);
            }
        }
    }
}
